package com.anuo.app.modules.coach.service;


import com.anuo.app.common.basedata.AuditState;
import com.anuo.app.common.basedata.BusinessType;
import com.anuo.app.common.entity.LocationFilter;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.thirdparty.PictureUtil;
import com.anuo.app.common.utils.DateUtil;
import com.anuo.app.modules.coach.dao.CoachDao;
import com.anuo.app.modules.coach.entity.Coach;
import com.anuo.app.modules.coach.entity.CoachCourse;
import com.anuo.app.modules.coach.entity.CoachFilter;
import com.anuo.app.modules.coach.entity.CoachStudentmoney;
import com.anuo.app.modules.system.entity.SystemFilter;
import com.anuo.app.modules.system.service.FileService;
import com.anuo.app.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class CoachService {
    @Autowired
    CoachDao coachDao;
    @Autowired
    UserService userService;
    @Autowired
    FileService fileService;

    @Transactional(readOnly = true)
    public Coach getCoachById(String coachId) {
        return coachDao.getCoachById(coachId);
    }

    /**
     * 删除已拒绝的教练申请
     *
     * @param coachId
     * @return
     */
    @Transactional(readOnly = false)
    public void deleteRefuesCoachApply(String coachId) {
        Coach coach = new Coach();
        coach.setState(AuditState.REFUSE);
        coach.setId(coachId);
        coachDao.deleteCoachApplay(coach);
    }

    @Transactional(readOnly = false)
    public void saveCoach(Coach coach) {
        CoachFilter coachFilter = new CoachFilter();
        coachFilter.setId(coach.getId());
        Coach coachdb = coachDao.getCoachApply(coachFilter);
        if (coachdb == null) {
            //添加操作
            coach.setId(coach.getId());
            coach.setCreatedate(new Date());
            coachDao.insertCoachApply(coach);
        } else {
            //修改操作
            coachDao.updateCoachApply(coach);
        }
    }

    @Transactional(readOnly = true)
    public boolean isHaveAuditInfo(CoachFilter coachFilter) {
        return coachDao.isHaveAuditInfo(coachFilter);
    }

    @Transactional(readOnly = true)
    public BigDecimal getCoachAVGScore(CoachFilter coachFilter) {
        return coachDao.getCoachAVGScore(coachFilter);
    }

    @Transactional(readOnly = true)
    public boolean isExistCoachInfo(String coachId) {
        CoachFilter coachFilter = new CoachFilter();
        coachFilter.setCoachid(coachId);
        return coachDao.isExistCoachInfo(coachFilter);
    }

    @Transactional(readOnly = true)
    public Coach getCoach(CoachFilter coachFilter) {
        return coachDao.getCoach(coachFilter);
    }

    @Transactional(readOnly = true)
    public String checkTimeValid(Date beginTime, Date endTime) {
        String beginTimeMaxStr = DateUtil.dateToStr(beginTime, "yyyy-MM-dd 22:00:00");
        Date beginTimeMax = DateUtil.strToDate(beginTimeMaxStr);
        String endTimeMaxStr = DateUtil.dateToStr(endTime, "yyyy-MM-dd 23:00:00");
        Date endTimeMax = DateUtil.strToDate(endTimeMaxStr);
        Date currentDate = new Date();
        if (DateUtil.getDistanceOfTwoDate(beginTimeMax, endTimeMax) > 0) {
            return "课程的时间不能跨天";
        } else if (beginTime.before(currentDate)) {
            return "课程的开始时间要大于当前时间";
        } else if (endTime.before(beginTime)) {
            return "课程的结束时间要大于开始时间";
        } else if (beginTime.after(beginTimeMax)) {
            return "课程的开始时间不能超过 晚上10点";
        } else if (endTime.after(endTimeMax)) {
            return "课程的结束时间不能超过 晚上11点";
        } else {
            return "";
        }

    }

    @Transactional(readOnly = true)
    public boolean isRepeatPeriodInPrivateCoach(CoachCourse coachCourse) {

        return coachDao.isRepeatPeriodInPrivateCoach(coachCourse);
    }

    @Transactional(readOnly = true)
    public boolean isCoach(Coach coach) {
        return coachDao.isCoach(coach);
    }

    @Transactional(readOnly = true)
    public List<CoachCourse> getCoachSyllabus(CoachFilter coachFilter) {
        return coachDao.getCoachSyllabus(coachFilter);
    }

    @Transactional(readOnly = true)
    public PageData getNearbyCoachList(CoachFilter coachFilter) {
        //获取附近的矩形范围
        LocationFilter locationRectangle = userService.getCurrentLocationRectangle(coachFilter);
        coachFilter.setLatitudeMax(locationRectangle.getLatitudeMax());
        coachFilter.setLatitudeMin(locationRectangle.getLatitudeMin());
        coachFilter.setLongitudeMax(locationRectangle.getLongitudeMax());
        coachFilter.setLongitudeMin(locationRectangle.getLongitudeMin());
        PageData<Coach> pageData = new PageData<>();
        //获取分页数据
        PageData.setTotalRow(pageData, coachFilter, () -> coachDao.getNearbyCoachListCount(coachFilter));
        PageData.setEntities(pageData, coachDao.getNearbyCoachList(coachFilter));
        return pageData;
    }

    private void setCoachList(List<Coach> list) {
        for (Coach coach : list) {
            if (coach.getBegin_teaching_date() != null) {
                int coageAge = DateUtil.getCurrentYearInt() - DateUtil.getYearInt(coach.getBegin_teaching_date());
                coach.setCoach_age(coageAge);
            }

        }
    }

    /**
     * 获取教练列表(经典分页实例)
     * @param coachFilter
     * @return
     */
    @Transactional(readOnly = true)
    public PageData<Coach> getCoachList(CoachFilter coachFilter) {
        PageData<Coach> pageData = new PageData<>();
        PageData.setTotalRow(pageData, coachFilter, () -> coachDao.getCoachListCount(coachFilter));
        PageData.setEntities(pageData, coachDao.getCoachList(coachFilter));
        return pageData;
    }

    @Transactional(readOnly = true)
    public Coach getCoachDetail(CoachFilter coachFilter) {
        return coachDao.getCoachDetail(coachFilter);
    }

    @Transactional(readOnly = true)
    public Coach getCoachApply(CoachFilter coachFilter) {
        //获取教练申请信息
        Coach coach = coachDao.getCoachApply(coachFilter);
        if (coach == null) {
            return null;

        }
        //获取教练文件信息
        SystemFilter systemFilter = new SystemFilter();
        systemFilter.setMaster_id(coach.getId());
        systemFilter.setMaster_type(BusinessType.COACH);
        coach.setFileinfoList(fileService.getFileinfoList(systemFilter));
        //获取教练文件的云url
        fileService.getFileinfoCloudUrlList(coach.getFileinfoList()
                , coachFilter.getPictureWidth()
                , coachFilter.getPictureHigh());
        //获取教练头像
        String cloudHeadUrl = PictureUtil.getPictureUrl(coach.getHeadurl()
                , coachFilter.getPictureWidth()
                , coachFilter.getPictureHigh());
        coach.setHeadurl(cloudHeadUrl);
        return coach;
    }

    @Transactional(readOnly = false)
    public void insertCoachMoneyFromNotPay(String id) {
        CoachStudentmoney coachStudentmoney = new CoachStudentmoney();
        coachStudentmoney.setId(id);
        coachDao.insertCoachMoneyFromNotPay(coachStudentmoney);
    }

    @Transactional(readOnly = false)
    public void updateCoachStudentMoneyToFormal(String id) {
        CoachStudentmoney coachStudentmoney = new CoachStudentmoney();
        coachStudentmoney.setPay(true);
        coachStudentmoney.setId(id);
        coachDao.updateCoachStudentMoneyToFormal(coachStudentmoney);
    }

    @Transactional(readOnly = false)
    public void deleteCoachStudentMoneyNotPay(String id) {
        CoachStudentmoney coachStudentmoney = new CoachStudentmoney();
        coachStudentmoney.setId(id);
        coachDao.deleteCoachStudentMoneyNotPay(coachStudentmoney);
    }

    @Transactional(readOnly = true)
    public CoachStudentmoney getCoachStudentMoney(CoachFilter coachFilter) {
        CoachStudentmoney coachStudentmoney = new CoachStudentmoney();
        coachStudentmoney.setId(coachFilter.getCoachStudentmoney_id());
        return coachDao.getCoachStudentMoney(coachStudentmoney);
    }
}